package com.hhkj.study.admin.goods.service.extend.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hhkj.study.admin.goods.entity.GoodsSku;
import com.hhkj.study.admin.goods.model.podo.GoodsPo;
import com.hhkj.study.admin.goods.model.podo.GoodsSkuDo;
import com.hhkj.study.admin.goods.model.podo.GoodsSkuPo;
import com.hhkj.study.admin.goods.model.res.GoodsSkuAssocGoodRes;
import com.hhkj.study.admin.goods.service.base.GoodsSkuService;
import com.hhkj.study.admin.goods.service.extend.GoodsExtendService;
import com.hhkj.study.admin.goods.service.extend.GoodsSkuExtendService;
import com.hhkj.study.common.cache.GlobalCache;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.enums.DictCodeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
*
* @author SERVER
* @since 2024-09-18
*/
@Service
public class GoodsSkuExtendServiceImpl extends BaseExtendServiceImpl<GoodsSku, GoodsSkuDo, GoodsSkuPo, GoodsSkuService> implements GoodsSkuExtendService {

    @Resource
    private GoodsExtendService goodsExtendService;

    @Resource
    private GlobalCache globalCache;

    @Override
    public List<GoodsSkuAssocGoodRes> supplyGoodsSkuGoodsByGoodsIdList(Collection<? extends Serializable> goodsSkuIdList, Collection<? extends Serializable> goodsIdList, Long goodCourseType) {
        if (CollectionUtil.isEmpty(goodsSkuIdList)) {
            return Lists.newArrayList();
        }
        List<GoodsSku> goodsSkuList = this.getService().listByIds(goodsSkuIdList);
        if (CollectionUtil.isEmpty(goodsSkuList)) {
            return Lists.newArrayList();
        }
        List<GoodsSkuAssocGoodRes> skuAssocGoodResList = BeanUtil.copyToList(goodsSkuList, GoodsSkuAssocGoodRes.class);
        if (CollectionUtil.isEmpty(goodsIdList)) {
            return Lists.newArrayList();
        }
        List<GoodsPo> goodsList = goodsExtendService.listByIdList(goodsIdList);
        if (CollectionUtil.isNotEmpty(goodsList)) {
            if (ObjectUtil.isNotNull(goodCourseType)) {
                goodsList = goodsList.stream().filter(e -> goodCourseType.equals(e.getCategoryId())).collect(Collectors.toList());
                if (CollectionUtil.isEmpty(goodsList)) {
                    return Lists.newArrayList();
                }
                Set<Long> filterGoodsIdSet = goodsList.stream().map(GoodsPo::getId).collect(Collectors.toSet());
                skuAssocGoodResList = skuAssocGoodResList.stream().filter(skuGood -> filterGoodsIdSet.contains(skuGood.getGoodsId())).collect(Collectors.toList());
            }
        }

        Map<Long, GoodsPo> goodsMap = goodsList.stream().collect(Collectors.toMap(GoodsPo::getId, Function.identity()));
        Map<Long, String> dictMap = globalCache.cacheSimpleDictMap(Lists.newArrayList(DictCodeEnum.COURSE_TYPE.getCode()));
        skuAssocGoodResList.forEach(goodsSku -> {
            GoodsPo goodsPo = goodsMap.get(goodsSku.getGoodsId());
            if (ObjectUtil.isNotNull(goodsPo)) {
                goodsSku.setValidStartTime(goodsPo.getValidStartTime());
                goodsSku.setValidEndTime(goodsPo.getValidEndTime());
                goodsSku.setGoodsName(goodsPo.getName());
                goodsSku.setCategoryParentName(dictMap.get(goodsPo.getCategoryId()));
            }
        });
        return skuAssocGoodResList;
    }

    @Override
    public Map<Long, String> selectAllGoodsSkuNameMap(Long goodsId) {
        GoodsSkuDo goodsSkuDo = new GoodsSkuDo();
        goodsSkuDo.setGoodsId(goodsId);
        goodsSkuDo.setGoodsPublishedFlag(SysConst.Flag.IS.getFlag());
        List<GoodsSkuPo> goodsSkuPoList = this.getService().listPoByDo(goodsSkuDo);
        if (CollectionUtil.isEmpty(goodsSkuPoList)) {
            return Maps.newHashMap();
        }
        return goodsSkuPoList.stream().collect(Collectors.toMap(GoodsSkuPo::getId, sku -> StrUtil.format("{}-{}", sku.getGoodsName(), sku.getName())));
    }
}
